package br.com.felix.dbm.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JToggleButton;

import br.com.felix.dbm.controller.ColumnController;
import br.com.felix.dbm.database.Column;
import br.com.felix.dbm.database.DataSize;
import br.com.felix.dbm.ui.components.ColumnJTextField;
import br.com.felix.dbm.ui.components.DataSizeJTextField;
import br.com.felix.dbm.ui.components.DataTypesComboBox;
import br.com.felix.dbm.ui.components.DeleteJButton;
import br.com.felix.dbm.ui.components.NullableCheckBox;

/**
 * User interface for a column.
 * */
public class ColumnUi extends javax.swing.JPanel {

	private static final long serialVersionUID = -9121818855742338793L;
	protected ColumnController columnController;
	protected ColumnJTextField txtColumnName;
	protected DataTypesComboBox cboDatatype;
	protected DataSizeJTextField dsTextField;
	protected NullableCheckBox chkAllowNull;
	protected JToggleButton tglPrimaryKey = new JToggleButton("PK");
	protected DeleteJButton btnDelete = new DeleteJButton("del");

	protected JLabel lblInfos = new JLabel("");



	public void setColumnController(ColumnController columnController) {
		this.columnController = columnController;
		txtColumnName = new ColumnJTextField(columnController);
		cboDatatype = new DataTypesComboBox(columnController);
		dsTextField = new DataSizeJTextField(columnController);
		chkAllowNull = new NullableCheckBox(columnController);
		configureDefaults();
	}



	protected void configureDefaults() {
		btnDelete.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				columnController.delete();
			}
		});
		btnDelete.setFont(Style.getTextFont());
		btnDelete.setPreferredSize(Style.getSmallButtonSize());

		tglPrimaryKey.setForeground(tglPrimaryKey.getBackground());
		tglPrimaryKey.setPreferredSize(Style.getSmallButtonSize());
		tglPrimaryKey.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				changeToggleButtonColor();
				columnController.setPrimaryKey(tglPrimaryKey.isSelected());
			}
		});

		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.NONE;
		gbc.gridx = 0;
		gbc.gridy = 0;
		add(tglPrimaryKey, gbc);
		gbc.gridx++;
		gbc.fill = GridBagConstraints.VERTICAL;
		add(txtColumnName, gbc);
		gbc.fill = GridBagConstraints.NONE;
		gbc.gridx++;
		add(cboDatatype, gbc);
		gbc.gridx++;
		add(dsTextField, gbc);
		gbc.gridx++;
		add(chkAllowNull, gbc);
		gbc.gridx++;
		add(lblInfos, gbc);
		gbc.gridx++;
		add(btnDelete, gbc);

		setVisible(true);
	}



	public void updateColumnName(String novoNome) {
		txtColumnName.setText(novoNome);
	}



	public void updateView(Column column) {
		tglPrimaryKey.setSelected(column.isPrimaryKey());
		changeToggleButtonColor();
		txtColumnName.setText(column.getName());
		this.cboDatatype.setSelectedItem(column.getType());
		DataSize size = column.getSize();
		if (size != null)
			this.dsTextField.setText(size.toString());
		chkAllowNull.setSelected(column.isNullAlowed());
	}



	private void changeToggleButtonColor() {
		if (tglPrimaryKey.isSelected()) {
			tglPrimaryKey.setForeground(Color.black);
		}
		else {
			tglPrimaryKey.setForeground(tglPrimaryKey.getBackground());
		}
	}
	
	
	public String toString(){
		return "ColumnUi for " + columnController.getModel().getName();
	}
}
